//
//  UploadFileAPI.m
//  CarLoan
//
//  Created by Mac on 2018/12/11.
//  Copyright © 2018年 eduonline. All rights reserved.
//

#import "UploadFileAPI.h"

@interface UploadFileAPI()

@property (nonatomic ,strong) NSString *theApiKey;

@property (nonatomic ,strong) NSMutableDictionary *argumentsDic;

@property (nonatomic ,assign) RequestMethod methodType;
//文件路径
@property (nonatomic ,copy)NSString *filePath;
//图片
@property(nonatomic ,strong)UIImage *image;
//图片名称
@property(nonatomic ,copy)NSString *imageName;
@end


@implementation UploadFileAPI{
    
}
//图片上传
- (instancetype)initWithAPIKey:(NSString *)apiKey
                      argument:(NSDictionary *)arguments
                         image:(UIImage *)image
                     imageName:(NSString *)imageName{
    
    self = [super init];
    
    if (self) {
        
        _theApiKey = @"";
        _argumentsDic = [NSMutableDictionary dictionary];
        _image = image;
        _imageName = imageName;
        if ([apiKey length]>0) {
            _theApiKey = apiKey;
        }
        
        if (arguments) {
            [_argumentsDic addEntriesFromDictionary:arguments];
        }
    }
    
    return self;
}
//文件上传
- (instancetype)initWithAPIKey:(NSString *)apiKey
                      argument:(NSDictionary *)arguments
                         filePath:(NSString *)filePath
{
    self = [super init];
    
    if (self) {
        
        _theApiKey = @"";
        _argumentsDic = [NSMutableDictionary dictionary];
        _filePath = filePath;
        if ([apiKey length]>0) {
            _theApiKey = apiKey;
        }
        
        if (arguments) {
            [_argumentsDic addEntriesFromDictionary:arguments];
        }
    }
    
    return self;
}
- (NSString *)requestUrl {
    return _theApiKey;
}

- (RequestMethod)requestMethod {
    return RequestMethodPOST;
}

- (id)requestArgument {
    
    /*NSString *_sid = [self getUserUUID];
     NSString *_t = [self getCurrentMinSecondTimestamp];
     NSString *_sign = [self GethashStrWithKey:[NSString stringWithFormat:@"%@&%@&%@",_sid,@"",_t]];
     NSDictionary *dic = @{@"loginMethod":@"2",
     @"_sign":_sign,
     @"_sid":_sid,
     @"_t":_t};
     
     [_argumentsDic addEntriesFromDictionary:dic];*/
    
    return _argumentsDic;
}

- (AFConstructingBlock)constructingBodyBlock{
    kWeakSelf;
    return ^(id formData) {
        if(weakSelf.filePath.length>0){
            NSLog(@"=====>上传文件");
            NSData *fileData = [NSData dataWithContentsOfFile:weakSelf.filePath];
            NSString *timeStamp = [NSString stringWithFormat:@"%ld",(long)([[NSDate date] timeIntervalSince1970]*1000)];
            NSString *str = [[self randomStringWithLength:5] stringByAppendingString:timeStamp];
            NSString *fileName = [self.filePath lastPathComponent];
            NSString *formKey = @"file";
            NSString *type = @"multipart/form-data";
            [formData appendPartWithFileData:fileData name:formKey fileName:fileName mimeType:type];
            
        }else{
            NSLog(@"=====>上传图片");
            NSData *data = UIImageJPEGRepresentation(weakSelf.image,1.0);
            NSString *timeStamp = [NSString stringWithFormat:@"%ld",(long)([[NSDate date] timeIntervalSince1970]*1000)];
            NSString *str = [[self randomStringWithLength:5] stringByAppendingString:timeStamp];
            NSString *name = [NSString stringWithFormat:@"%@.jpg",str];
            NSString *formKey = @"file";
            NSString *type = @"image/jpg";
            [formData appendPartWithFileData:data name:formKey fileName:self.imageName?self.imageName:name mimeType:type];
        }
        
    };
}

- (NSTimeInterval)requestTimeoutInterval{
    return 300;
}

#pragma mark - 辅助方法,模拟生成随机数
- (NSString *)randomStringWithLength:(int)len {
    NSString *letterd =@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
    
    for (NSUInteger i = 0; i < len; i++) {
        u_int32_t r = arc4random() % [letterd length];
        unichar c = [letterd characterAtIndex:r];
        [randomString appendFormat:@"%C", c];
    }
    NSLog(@"random string:%@",randomString);
    return randomString;
}

@end
